﻿# -*- coding: utf-8 -*

INN_TEST_WEIGHTS_10 = (2,4,10,3,5,9,4,6,8,0)
INN_TEST_WEIGHTS_12_0 = (7,2,4,10,3,5,9,4,6,8,0,0)
INN_TEST_WEIGHTS_12_1 = (3,7,2,4,10,3,5,9,4,6,8,0)

def test_inn_org(inn):
    """ returns True if inn of the organisation pass the check
    """
    if len(inn) != 10:
        return False

    r = 0
    for i, c in enumerate(inn):
        r += int(c) * TEST_WEIGHTS_10[i]
    r = r % 11 % 10

    return r == int(inn[-1])

def test_inn_person(inn):
    """ returns True if inn of the person pass the check
    """
    if len(inn) != 12:
        return False

    r1 = 0
    for i, c in enumerate(inn):
        r1 += int(c) * TEST_WEIGHTS_12_0[i]
    r1 = r1 % 11 % 10
    
    r2 = 0
    for i, c in enumerate(inn):
        r2 += int(c) * TEST_WEIGHTS_12_1[i]
    r2 = r2 % 11 % 10

    return (r1 == int(inn[-2])) and (r2 == int(inn[-1]))

def test_inn(inn):
    """ returns True if inn pass the check
    """
    if len(inn) == 10:
        return test_inn_org(inn)
    elif len(inn) == 12:
        return test_inn_person(inn)

    return False

BANK_ACC_TEST_WEIGHTS = (7,1,3,7,1,3,7,1,3,7,1,3,7,1,3,7,1,3,7,1,3,7,1)

def test_bank_number(anumber):
    if not isinstance(anumber, str) and not anumber.isdigit():
        return False

    if len(anumber) != 23:
        return False

    r = 0
    for i, c in enumerate(anumber):
        r += int(c) * BANK_ACC_TEST_WEIGHTS[i]
    return (r % 10) == 0 

def test_bank_corr_account_number(account, bik):
    return len(account) == 20 and len(bik) == 7 and \
        test_bank_number('0' + bik[4:6] + account)

def test_bank_account_number(account, bik):
    return len(account) == 20 and len(bik) == 7 and \
        test_bank_number(bik[-3:] + account)
